package com.huahang.fashionpress.controller;

import com.huahang.fashionpress.enums.ExceptionEnum;
import com.huahang.fashionpress.exception.ResultException;
import com.huahang.fashionpress.form.FormAdmin;
import com.huahang.fashionpress.mapper.AdminMapper;
import com.huahang.fashionpress.model.Admin;
import com.huahang.fashionpress.service.AdminService;
import com.huahang.fashionpress.util.Md5Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.net.ssl.HttpsURLConnection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

/**
 * 后台登录控制
 * @Author: 阿俊哥
 * @Date: 2019/8/23 11:58
 * @Version 1.0
 */
@Controller
@Slf4j
@RequestMapping("/admin")
public class AdminLoginController {

    @Resource
    AdminService adminService;

    /**
     * 后台主入口
     * @return
     */
    @GetMapping("/")
    public ModelAndView login(){
        return new ModelAndView("admin/login");
    }

    /**
     * 登录验证表单后进行数据的验证
     * @param formAdmin
     * @return
     */
    @PostMapping("/login")
    public ModelAndView login(FormAdmin formAdmin, HttpSession session, Map<String,Object> map){
        //拿到表单数据 进行数据的比对
        if(formAdmin==null){
            log.info("【后台用户管理】数据有误");
            throw new ResultException(ExceptionEnum.DATA_ERROR);
        }
        Admin admin = adminService.selectAdminByName(formAdmin.getAdminUsername());
        //判断密码是否相同
        if(!Md5Util.getMd5String(formAdmin.getAdminPassword()).equals(admin.getAdminPassword())){
            log.info("【后台用户管理】登录时密码错误，错误密码是："+Md5Util.getMd5String(formAdmin.getAdminPassword()));
            map.put("msg", "密码错误！");
            map.put("formAdmin",formAdmin);
            return new ModelAndView("/admin/login",map);
        }

        session.setAttribute("administrator", admin);
        return new ModelAndView("redirect:/admin/index",map);

    }

    @RequestMapping(value = "/logout",method = RequestMethod.GET)
    public ModelAndView logout(HttpServletRequest request){

        HttpSession session = request.getSession();
        session.removeAttribute("administrator");
        session.removeAttribute("order");
        session.removeAttribute("category");
        session.removeAttribute("img");
        session.removeAttribute("product");
        session.removeAttribute("nick");
        session.removeAttribute("admin");
        return new ModelAndView("redirect:/admin/");
    }



    /**
     * 忘记密码入口
     * @return
     */
    @RequestMapping(value = "/forgotPassowrd",method = RequestMethod.GET)
    public ModelAndView forgotPassword(){
        return new ModelAndView("/admin/login/forgot-password");
    }

    /**
     * 找回密码验证用户名和注册的手机号
     * @param adminName
     * @param adminPhone
     * @param map
     * @return
     */
    @RequestMapping(value = "/checkadminphone",method = RequestMethod.POST)
    public ModelAndView checkadminphone(String adminName,String adminPhone,Map<String,Object> map){



        Admin admin = adminService.selectAdminByName(adminName);
        if(!admin.getAdminPhone().equals(adminPhone)){
            log.info("【后台用户管理】 找回密码手机号参数错误！");
            map.put("msg", "手机号不正确");
            return new ModelAndView("/admin/login/forgot-password", map);
        }
        map.put("adminName", adminName);
        return new ModelAndView("/admin/login/checkPhone",map);

    }

    @RequestMapping(value = "/forgotPassword",method = RequestMethod.POST)
    public ModelAndView forgotPassword(FormAdmin formAdmin,Map<String,Object> map){
        Admin admin = adminService.selectAdminByName(formAdmin.getAdminUsername());
        admin.setAdminPassword(Md5Util.getMd5String(formAdmin.getAdminPassword()));
        adminService.updateAdmin(admin);
        map.put("formAdmin", formAdmin);
        return new ModelAndView("/admin/login", map);
    }


    /**
     * 检测用户是否可用
     * @param userUsername
     * @param response
     */
    @RequestMapping("/checkusername/{userUsername}")
    public void checkUserName(@PathVariable String userUsername, HttpServletResponse response)  {
        if(userUsername==null||userUsername==""){
            log.info("【后台用户管理】 找回密码参数错误！");
            throw new ResultException(ExceptionEnum.PARAM_ERROR);
        }
        Admin admin = adminService.selectAdminByName(userUsername);
        response.setContentType("text/html;charset=utf-8");
        if(admin==null) {
            try {
                response.getWriter().write("此用户不存在");
            } catch (IOException e) {
            }
        }
        else {
            try {
                response.getWriter().write("此用户可用");
            } catch (IOException e) {

            }
        }
    }



}
